package top.drinyeit.weilong.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;

import java.util.List;

import top.drinyeit.weilong.R;
import top.drinyeit.weilong.model.MessageBean;

/**
 * Created by wu on 2018/1/5.
 */

public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.ViewHolder> {

    private List<MessageBean> list;
    private Context context;

    public MessageAdapter(List<MessageBean> list) {
        this.list = list;
    }

    public OnItemClickListener itemClickListener;

    public void setOnItemClickListener(OnItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public ItemLongClick itemLongClick;

    public void setItemLongClick(ItemLongClick itemLongClick) {
        this.itemLongClick = itemLongClick;
    }

    public interface ItemLongClick {
        void ItemLongClick(View view, int position);
    }


    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
        public ImageView shop_im;
        private ImageView isRead;
        public TextView theme, time, msg;

        public ViewHolder(View itemView) {
            super(itemView);
            shop_im = itemView.findViewById(R.id.shop_im);
            isRead = itemView.findViewById(R.id.isRead);
            theme = itemView.findViewById(R.id.theme);
            time = itemView.findViewById(R.id.time);
            msg = itemView.findViewById(R.id.msg);

            itemView.setOnClickListener(this);
            itemView.setOnLongClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (itemClickListener != null) {
                itemClickListener.onItemClick(v, getAdapterPosition());
            }
        }

        @Override
        public boolean onLongClick(View v) {
            if (itemLongClick != null) {
                itemLongClick.ItemLongClick(v, getAdapterPosition());
            }
            return true;
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        context = parent.getContext();
        View v = LayoutInflater.from(context).inflate(R.layout.item_message, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Glide.with(context).load(list.get(position).getMessage_source_head_photo())
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .placeholder(R.drawable.ic_def)
                .dontTransform()
                .into(holder.shop_im);
        holder.msg.setText(list.get(position).getMessage_content());
        holder.theme.setText(list.get(position).getMessage_source_name());
        holder.time.setText(list.get(position).getCreate_time());
        if (list.get(position).getState() == 0){
            holder.isRead.setVisibility(View.VISIBLE);
        }else {
            holder.isRead.setVisibility(View.GONE);
        }
    }

    public void removeItem(int pos) {
        list.remove(pos);
        notifyItemRemoved(pos);
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }
}
